package com.h.chatroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h.chatroom.common.constant.Constants;
import com.h.chatroom.common.exception.ChatroomException;
import com.h.chatroom.common.util.BeanTransUtils;
import com.h.chatroom.common.util.JwtUtils;
import com.h.chatroom.common.util.SecurityUtils;
import com.h.chatroom.mapper.UserMapper;
import com.h.chatroom.pojo.dataobject.User;
import com.h.chatroom.pojo.request.UserLoginRequest;
import com.h.chatroom.pojo.request.UserRegisterRequest;
import com.h.chatroom.pojo.response.UserInfoResponse;
import com.h.chatroom.pojo.response.UserLoginResponse;
import com.h.chatroom.pojo.response.UserRegisterResponse;
import com.h.chatroom.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    // 登录
    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,userLoginRequest.getUsername())
                .eq(User::getDeleteFlag, Constants.BLOG_NORMAL);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new ChatroomException("用户不存在");
        }
        // 需要使用到 令牌 和 加盐 操作
        // 判断查询到的密码是否正确
        // 使用到了加盐操作
        if(!SecurityUtils.verify(userLoginRequest.getPassword(),user.getPassword())) {
            throw new ChatroomException("用户密码错误");
        }
        // 密码正确，进行创建令牌，并进行返回
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user.getUserId());
        map.put("username",user.getUsername());
        String token = JwtUtils.getToken(map);

        return new UserLoginResponse(user.getUserId(),user.getUsername(),token);
    }

    // 注册
    @Override
    public UserRegisterResponse register(UserRegisterRequest userRegisterRequest) {
        User user = null;
        try {
            user = new User();
            user.setUsername(userRegisterRequest.getUsername());
            user.setPassword(SecurityUtils.encrypt(userRegisterRequest.getPassword()));
            int ret = userMapper.insert(user);
            log.info("注册 ret: " + ret);
        }catch (DuplicateKeyException e) {
            // 如果 insert 出现异常的话，说明名字重复，注册失败
            log.error("注册失败 username: " + userRegisterRequest.getUsername());
        }
        return new UserRegisterResponse(user.getUserId(),user.getUsername());
    }

    // 获取登录用户信息
    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserId,userId)
                .eq(User::getDeleteFlag,Constants.BLOG_NORMAL);
        User user = userMapper.selectOne(queryWrapper);
        return BeanTransUtils.trans(user);
    }
}
